﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;

namespace BDSim
{
    /// <summary>
    /// SetTime.xaml 的交互逻辑
    /// </summary>
    public partial class ScenarioParamConfigWnd : Window, INotifyPropertyChanged
    {
        public ScenarioParamConfigWnd()
        {
            InitializeComponent();
        }

        #region 属性及成员

        //string _StartTimeStr = string.Format("{0}-{1:00}-{2:00} {3:00}:{4:00}:{5:00}", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, 0, 0);
        string _StartTimeStr = "2015-10-01 12:00:00";

        public string StartTimeStr
        {
            get { return _StartTimeStr; }
            set {
                if (DateTime.TryParse(value, out tempTime))
                {
                    _StartTimeStr = value;
                    //如果开始时间晚于场景历元，则设置场景历元等于开始时间
                    if (DateTime.Parse(value).CompareTo(DateTime.Parse(EpochStr)) > 0)
                    {
                        EpochStr = value;
                        NotifyProperty("StartTimeStr");
                    }
                }
                else
                    MessageBox.Show("输入有误，请参照给定格式输入！", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        //string _StopTimeStr = string.Format("{0}-{1:00}-{2:00} {3:00}:{4:00}:{5:00}", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour + 1, 0, 0);
        string _StopTimeStr = "2015-10-01 14:00:00";

        public string StopTimeStr
        {
            get { return _StopTimeStr; }
            set {
                if (DateTime.TryParse(value, out tempTime))
                {
                    _StopTimeStr = value;
                    //如果结束时间早于场景历元，则设置场景历元等于开始时间
                    if (DateTime.Parse(value).CompareTo(DateTime.Parse(EpochStr)) < 0)
                    {
                        EpochStr = StartTimeStr;
                        NotifyProperty("StopTimeStr");
                    }
                }
                else
                    MessageBox.Show("输入有误，请参照给定格式输入！", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        string _ScenarioEpoch = "2015-10-01 12:00:00";

        public string EpochStr
        {
            get { return _ScenarioEpoch; }
            set
            {
                if (DateTime.TryParse(value, out tempTime))
                {
                    //if (DateTime.Parse(value).CompareTo(DateTime.Parse(StartTimeStr)) >= 0 && DateTime.Parse(value).CompareTo(DateTime.Parse(StopTimeStr)) < 0)
                    //{
                        _ScenarioEpoch = value;
                        NotifyProperty("EpochStr");
                    //}
                    //else
                    //{
                    //    MessageBox.Show("场景历元设置必须在开始时间与结束时间之间!", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                    //}
                }
                else
                    MessageBox.Show("输入有误，请参照给定格式输入！", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        double _SimStep = 60;

        public double SimStep
        {
            get { return _SimStep; }
            set {
                if (value > 0 && value <= 600)
                {
                    _SimStep = value;
                    NotifyProperty("SimStep");
                }
                else
                    MessageBox.Show("输入有误，仿真步长设置需大于0，小于等于600s！", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        public DateTime tempTime = new DateTime();


        #endregion

        #region 确定建立仿真场景
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (DateTime.Parse(EpochStr).CompareTo(DateTime.Parse(StartTimeStr)) >= 0 && DateTime.Parse(EpochStr).CompareTo(DateTime.Parse(StopTimeStr)) < 0)
            {
                this.DialogResult = true;
            }
            else
            {
                txtError.Visibility = System.Windows.Visibility.Visible;
                //MessageBox.Show("未能执行此操作，请确认是否存在以下问题：\r\n(1)开始时间在结束时间之后；\r\n(2)场景历元不在开始时间与结束时间之间。");
            }
        }
        #endregion

        #region 退出窗口
        private void button2_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
        #endregion

        public event PropertyChangedEventHandler PropertyChanged;

        public void NotifyProperty(string name)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(name));
            }
        }
    }
}
